/*
Driver.java
Autor: Paola Garcia, carnet 10205
Fecha: 02 septiembre 2011
Descripcion: clase principal que genera las acciones del arbol de huffman.
*/

import java.util.Iterator;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.List;

public class Driver{
	public static void main(String args[])
	{
	// read System.in one character at a time
	
	String ingreso;
	Scanner teclado = new Scanner(System.in);
	List<node> freq = new LinkedList<node>();
	// read data from input
	
	System.out.println("Ingrese una cadena:");	
	ingreso = teclado.nextLine();
	System.out.println("Tabla de codigo");
	System.out.println("Caracter     Frecuencia      Codigo");	
	System.out.println("");	
	
	while (teclado.hasNext())
	{
	// s.next() returns string; we're interested in first char
	char c = teclado.next().charAt(0);
	// look up character in frequency list
	node query = new node(c);
	}
	// insert each character into a Huffman tree
	LinkedList<HuffmanTree> trees = new LinkedList<HuffmanTree>();
	for (node n : freq)
	{
	trees.add(new HuffmanTree(n));
	}
	Iterator<HuffmanTree> ti;
	// merge trees in pairs until one remains
	while (trees.size() > 1)
	{
	// construct a new iterator
	ti = trees.iterator();
	// grab two smallest values
	HuffmanTree smallest = (HuffmanTree)ti.next();
	HuffmanTree small = (HuffmanTree)ti.next();
	// remove them
	trees.remove(smallest);
	trees.remove(small);
	// add bigger tree containing both
	trees.add(new HuffmanTree(smallest,small));
	}
	// print only tree in list
	ti = trees.iterator();
	HuffmanTree encoding = (HuffmanTree)ti.next();
	encoding.print();
	}
	
}